package com.zjw.zy.coreui.fragment.memorandum

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.text.Editable
import android.text.TextWatcher
import android.view.View
import android.widget.EditText
import android.widget.ScrollView
import android.widget.TextView
import androidx.fragment.app.viewModels
import com.zjw.zy.R
import com.zjw.zy.base.BaseFragment
import com.zjw.zy.base.ImageEditText
import com.zjw.zy.bean.MemorandumItem
import com.zjw.zy.constant.ImageSubPath
import com.zjw.zy.ext.*
import com.zjw.zy.utils.*
import com.zjw.zy.viewmodel.MemorandumViewModel
import java.util.*

/**
 * @author     ：zhong.jw
 * @date       ：Created in 2022/9/30 11:14
 * @description：备忘录编辑页面
 */
class MemorandumTextFragment : BaseFragment() {

    companion object {
        const val TEXT_DATA_KEY = "TEXT_DATA_KEY"
        private const val TAG = "MemorandumTextFragment"
        private const val ADD_MODE = 1
        private const val UPDATE_MODE = 2

        private val REQUEST_IMAGE = "MemorandumTextFragment_REQUEST_IMAGE".hashCode()
    }


    private val mMemorandumViewModel: MemorandumViewModel by viewModels()

    private val mDate: TextView by lazy { findViewById(R.id.memorandum_text_date) }
    private val mTitle: EditText by lazy { findViewById(R.id.memorandum_text_title) }
    private val mText: ImageEditText by lazy { findViewById(R.id.memorandum_text_text) }
    private val mScrollContent: ScrollView by lazy { findViewById(R.id.memorandum_text_content) }

    private val undoUtils: UndoUtils = UndoUtils("")

    //0 新增  1 编辑
    private var mType = ADD_MODE
    private lateinit var mData: MemorandumItem

    private var isShare = false

    private val mTextWatcher = object : TextWatcher {
        override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {

        }

        override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
            undoUtils.markChange(s.toString())
            WindowUtils.TopBar.setEnable2RightButton(undoUtils.canUndo())
        }

        override fun afterTextChanged(s: Editable?) {

        }

    }

    override fun onPageEnter() {
        WindowUtils.TopBar.showBack()
        WindowUtils.FootBar.hide()
    }

    override fun onViewRestore(savedInstanceState: Bundle?) {

    }

    override fun onSubscribe() {

    }

    override fun onBindViewListener() {
        mText.addTextChangedListener(mTextWatcher)
    }

    override fun onBindViewValue() {
        arguments?.getParcelable<MemorandumItem>(TEXT_DATA_KEY)?.let {
            LogUtils.d(TAG, "arguments no empty")
            mType = UPDATE_MODE
            mData = it
            mDate.text = it.date.dateString()
            mTitle.setText(it.title)
            mText.setImageWidth(600)
            mText.setTextWithImage(it.text)
            unEditMode()
        } ?: run {
            //如果传过来为空，默认新文件
            LogUtils.d(TAG, "arguments empty")
            mType = ADD_MODE
            mData = MemorandumItem(Date().time, "", "", "")
            mDate.text = mData.date.dateString()
            editMode()
        }

        undoUtils.markStart(mData.text)
    }

    override fun onFindView(rootView: View?) {

    }

    override fun onBackClick() {
        WindowUtils.TopBar.hide2RightButton()
        WindowUtils.TopBar.hide3RightButton()

        super.onBackClick()
    }

    override fun onLayoutId(): Int {
        return R.layout.fragment_memorandum_text
    }

    override fun onPageExit() {
        //隐藏右侧第二辅助按钮
    }


    private fun editMode() {

        //删除
        WindowUtils.TopBar.show4RightButton(ResourcesUtils.getDrawable(R.drawable.icon_delete)) {
            alertConfirm(message = ResourcesUtils.getString(R.string.delete_confirm)) {
                if (it) {
                    mMemorandumViewModel.deleteMemorandumData(mData)
                    back()
                }
            }
        }


        //图片
        WindowUtils.TopBar.show3RightButton(ResourcesUtils.getDrawable(R.drawable.icon_cramer_image)) {
            pickUpImage(PathUtils.getPrivateImagePath(ImageSubPath.MEMORANDUM_IMAGE_PATH)) {
                mText.appendImage(it)
            }
        }
        //撤销功能
        WindowUtils.TopBar.show2RightButton(ResourcesUtils.getDrawable(R.drawable.icon_undo)) {
            //取消上次修改
            undoUtils.undo(mText, mTextWatcher)
            WindowUtils.TopBar.setEnable2RightButton(undoUtils.canUndo())
        }
        WindowUtils.TopBar.setEnable2RightButton(false)

        //确认功能
        WindowUtils.TopBar.show1RightButton(ResourcesUtils.getDrawable(R.drawable.icon_done)) {
            //保存修改
            doneEdit()
        }

        mTitle.edit()
        mText.edit()
    }

    private fun unEditMode() {
        WindowUtils.TopBar.hide2RightButton()
        WindowUtils.TopBar.hide3RightButton()
        WindowUtils.TopBar.hide4RightButton()
        //进入编辑
        WindowUtils.TopBar.show1RightButton(ResourcesUtils.getDrawable(R.drawable.icon_edit)) {
            editMode()
        }
        mTitle.normal()
        mText.normal()
    }


    //完成
    private fun doneEdit() {
        //保存当前
        mData.text = mText.value()
        mData.title = mTitle.text.toString()

        //回滚起点
        undoUtils.markStart(mData.text)

        unEditMode()

        //如果为空，返回上一页
        if (mData.isEmpty) {
            mMemorandumViewModel.deleteMemorandumData(mData)
            back()
            return
        }

        if (mType == UPDATE_MODE) {
            mMemorandumViewModel.updateMemorandumData(mData)
        } else {
            mMemorandumViewModel.addMemorandumData(mData)
        }

        mType = UPDATE_MODE
    }
}